package com.dormms.www.contact.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dormms.www.contact.service.ContactService;
import com.dormms.www.dto.PageQueryObject;
import com.dormms.www.entity.Contacts;
import com.dormms.www.result.Result;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@RestController
@RequestMapping("/contact")
public class ContactController {

    @Resource
    private ContactService contactService;

    @GetMapping("/contactPage")
    public Result<?> studentsPage(PageQueryObject pageQueryObject){
        Page<Contacts> contacts = contactService.contactsSelectPage(pageQueryObject);
        return new Result<>().success().put(contacts);
    }

    @PostMapping("/addContact")
    public Result<?> addContact(@RequestBody Contacts addContact){
        boolean result = contactService.addContact(addContact);
        if(result){
            return new Result<>().success("成功添加了一条学生信息");
        }else {
            return new Result<>().error("学生信息添加失败");
        }
    }

    @PostMapping("/updateContact")
    public Result<?> updateContact(@RequestBody Contacts updateContact){
        boolean result = contactService.updateContact(updateContact);
        if(result){
            return new Result<>().success("成功修改了一条学生信息");
        }else {
            return new Result<>().error("学生信息修改失败");
        }
    }

    //删除学生
    @GetMapping("/deleteContact")
    public Result<?> deleteContact(Integer contactId){
        boolean result = contactService.removeById(contactId);
        if(result){
            return new Result<>().success("成功删除了一条学生信息");
        }else {
            return new Result<>().error("学生信息删除失败");
        }
    }
}
